<?php
/**
 * User: gli
 * Date: 13-3-11
 * Time: 9:00 PM
 */
class Variation extends Base {

	protected function init() {
		parent::init();

		$this->attributes['eid'] = 0;
		$this->attributes['name'] = '';
		$this->attributes['code'] = '';
	}

	public function __construct($id) {
		$this->init();

		if (!empty($id)) {
			if ($id instanceof stdClass) {
				if (!$this->load($id->id)) {
					throw new Exception("Can not load variation by given id: {$id->id}");
				}
			} else if ($id instanceof Variation) {
				foreach ($id->attributes as $key => $value) {
					$this->attributes[$key] = $value;
				}
			} else if (is_string($id) || is_numeric($id)) {
				if (!$this->load($id)) {
					throw new Exception("Can not load variation by given id: {$id}");
				}
			} else {
				throw new InvalidParameterException("Invalid parameter to get variation: {$id}");
			}
		}
	}

	protected function load($id) {
		$row = get_variation_object_as_row($id);
		if ($row) {
			$obj_array = (array) $row;
			foreach ($obj_array as $key => $value) {
				$this->attributes[$key] = $value;
			}
			return true;
		}
		return false;
	}

	protected function save() {
		return create_variation(
			$this->get('id'),
			$this->get('eid'),
			$this->get('name'),
			$this->get('code')
		);
	}
}

/**
 * Create or update variation
 * @param int $id variation id
 * @param int $eid experiment id
 * @param string $name variation name
 * @param string $code variation code
 *
 * @return bool|int
 */
function create_variation($id, $eid, $name, $code) {
	$id = (int) $id;
	$eid = (int) $eid;
	$name = mysql_real_escape_string($name);
	$code = mysql_real_escape_string($code);

	$time = time();
	$row = get_variation_object_as_row($id);
	if ($row) {
		$query = "UPDATE variations set eid={$eid}, name='{$name}', code='{$code}', updated={$time}";
		$result = update_data($query);
		if ($result != false) {
			return $id;
		}
	} else {
		$query = "INSERT INTO variations (eid, name, code, created, updated) VALUES($eid, '$name', '$code', $time, $time)";
		$id = insert_data($query);
		if ($id != false) {
			return $id;
		}
	}
	return false;
}

/**
 * Get variation by given id
 * @param int $id variation id
 *
 * @return bool|object
 */
function get_variation_object_as_row($id) {
	$id = (int) $id;
	if (!$id || $id < 0) return false;

	$query = "SELECT * FROM variations WHERE id={$id}";
	return get_data_row($query);
}

/**
 * Get variation object by given id
 * @param int $id variation id
 *
 * @return Variation|false
 */
function get_variation_by_id($id) {
	try {
		return new Variation($id);
	} catch (Exception $ex) {
		error_log("[get_variation_by_id] Can not get variation by given id: {$id}");
	}
	return false;
}

/**
 * Get variations by user id and project id
 * @param int $uid user id
 * @param int $pid project id
 * @param int $offset
 * @param int $limit
 *
 * @return array
 */
function get_variations($uid, $pid, $offset=0, $limit=10) {
	$uid = (int) $uid;
	$pid = (int) $pid;
	$offset = (int) $offset;
	$limit = (int) $limit;
	if (!$uid || !$pid) return array();
	if ($offset < 0) $offset = 0;
	if ($limit <= 0) $limit = DB_DEFAULT_LIMIT;

	$variations = array();
	$query = "SELECT * FROM variations v LEFT JOIN experiments e ON e.id=v.eid WHERE e.uid={$uid} AND e.pid={$pid} LIMIT {$offset}, {$limit}";
	$rows = get_data($query);
	if ($rows) {
		foreach ($rows as $row) {
			$variations[] = new Variation($row);
		}
	}
	return $variations;
}